package top.yesjava.webfinal.web;

import top.yesjava.webfinal.common.framework.simplemvc.RequestMapping;
import top.yesjava.webfinal.common.utils.JSONUtil;
import top.yesjava.webfinal.common.utils.PageInfo;
import top.yesjava.webfinal.common.utils.Result;
import top.yesjava.webfinal.entity.Student;
import top.yesjava.webfinal.entity.User;
import top.yesjava.webfinal.service.StudentService;
import top.yesjava.webfinal.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/stu") // "//login.do"
public class StudentController {
    StudentService studentService = new StudentService();

    @RequestMapping("/list.do")
    public String toPage(HttpServletRequest request, HttpServletResponse response) {
        return "/student/list";
    }

    @RequestMapping("/loadStudents.do")
    public void loadStudents(HttpServletRequest request, HttpServletResponse response) {
        String pageNoStr = request.getParameter("pageNo");
        Integer pageNo = (pageNoStr != null ? Integer.parseInt(pageNoStr) : 1);
        String pageSizeStr = request.getParameter("pageSize");
        Integer pageSize = (pageSizeStr != null ? Integer.parseInt(pageSizeStr) : 15);

        PageInfo<Student> pageInfo = studentService.findStudentsByPage(pageNo, pageSize);
        JSONUtil.printByJSON(response, Result.success(pageInfo));
    }


    @RequestMapping("/del.do")
    public void delStus(HttpServletRequest request, HttpServletResponse response) {
        String[] idsStr = request.getParameterValues("id");
        Integer[] ids = null;
        if(idsStr!=null && idsStr.length>0){
            ids = new Integer[idsStr.length];
            for (int i = 0; i < ids.length; i++) {
                ids[i] = Integer.parseInt(idsStr[i]);
            }
        }
        studentService.removeStudentById(ids);
        JSONUtil.printByJSON(response,Result.success());
    }





}
